// pages/publishActivitie/publishActivitie.js
var $ = require('../util/commit.js');
var config = require('../util/config.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: "",
    HDFZFStatus: false,
    HDFBodyStatus: false, //
    tabIndex: 0,
    title: '',
    TopImageHide: true,
    TopImage: '',
    pic_id: '',
    type: '',
    typeVal: '',
    address: '',
    detailed_address: '',
    address_x: '',
    address_y: '',
    price: '',
    collage_id: '',
    collage_val: '',

    collage_numberVal: '请选择下限和上限',
    collage_number_max: '',
    collage_number_min: '',
    price_val: '',
    price_id: '',
    num: '',
    showTypePicker: false,
    remark: "",
    typeData: [{
      id: 0,
      name: '基本信息'
    }, {
      id: 1,
      name: '活动详情'
    }, {
      id: 2,
      name: '完成发布'
    }],
    addrArray: [{
      id: 1,
      name: '线上活动'
    }, {
      id: 2,
      name: '线下活动'
    }],
    TypeArray: [],
    PsnNumArray: [],
    BMMoney: [],
    DateArray: [],
    DateIndex: [],
    dateStartTimeVal: '',
    dateEndTimeVal: '',
    content: [],
    sendRequire: {},
    remarkData:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options);
    let that = this;
    if (options.data){
      let param = JSON.parse(options.data);
      that.setData({
        param: param,
      });
      if (param.page == 'payPage') {

        that.setData({
          tabIndex: 2,
        })
      }
    }
   
    that.getDateArray();
    that.loadMFnum();
    that.loadPsnNumber();
  },
  // 加载时获取DateArray
  getDateArray: function() {
    let dateTimeArray = $.DateArray();
    this.setData({
      DateArray: dateTimeArray[0],
      DateIndex: dateTimeArray[1]
    });
    console.log(dateTimeArray);
  },
  // 加载免费次数
  loadMFnum: function(e) {
    let that = this;
    $.POST({
      url: 'caSUFR',
      data: {

      }
    }, function(e) {
      that.setData({
        num: e.data.num
      });
      if (e.data.sponsor==0){
        wx.showToast({
          title: '请先设置主办方',
          icon:'none'
        })
      }
    }, function(e) {})
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  // 选择拼团海报
  selectPhoto: function(e) {
    let that = this;
    $.openWin({
      url: '../poster/poster'
    })
    // that.setData({
    //   TopImageHide:false,
    //   TopImage:'https://app.xingxue.vip/image1_1/jpg02.jpg'
    // })
  },
  delPhoto: function(e) {
    let that = this;
    that.setData({
      TopImageHide: true,
      TopImage: '',
      pic_id: '',
    })
  },
  // 活动地点选择事件
  bindPickerAddChange: function(e) {
    if (this.data.addrArray[e.detail.value].id == 2) {
      this.setData({
        address: '',
        detailed_address: '',
        type: '',
        typeVal: '',
        address_x: '',
        address_y: '',
        showTypePicker: true,
      })
      $.openWin({
        url: '../activeAddress/activeAddress'
      })
    } else {
      this.setData({
        typeVal: this.data.addrArray[e.detail.value].name,
        address: this.data.addrArray[e.detail.value].name,
        detailed_address: '',
        type: this.data.addrArray[e.detail.value].id,
        address_x: '',
        address_y: '',
        showTypePicker: true,
      })
      this.loadTypeData();

    }

  },
  // 加载类别
  loadTypeData: function(e) {
    let that = this;
    console.log(that.data.type)
    $.POST({
      url: 'caSCC',
      data: {
        type: that.data.type,
      }
    }, function(e) {
      that.setData({
        TypeArray: e.data
      })

    }, function(e) {

    })
  },
  // 加载人数
  loadPsnNumber: function(e) {
    let that = this;
    let lowData=[],upData=[];
    for(var i=1; i<500;i++){
      lowData.push(i);
      upData.push(i);
    };
    
    let newPsnNumArray=[];
    newPsnNumArray.push(lowData);
    newPsnNumArray.push(upData);
    console.log(newPsnNumArray);
    that.setData({
      PsnNumArray: newPsnNumArray
    })
    // $.POST({
    //   url: 'caSCNL',
    //   data: {
    //     ccid: that.data.collage_id,
    //   }
    // }, function(e) {
    //   that.setData({
    //     PsnNumArray: e.data
    //   })

    // }, function(e) {

    // })
  },
  // 加载报名费
  loadBMFData: function(e) {
    let that = this;

    $.POST({
      url: 'caSCPL',
      data: {
        ccid: that.data.collage_id,
      }
    }, function(e) {
      that.setData({
        BMMoney: e.data
      })

    }, function(e) {

    })
  },
  // 选择类别
  bindPickerTypeChange: function(e) {
    let that = this;

    this.setData({
      collage_val: that.data.TypeArray[e.detail.value].title,
      collage_id: that.data.TypeArray[e.detail.value].collage_id,
      price: that.data.TypeArray[e.detail.value].price,
    });

    that.loadBMFData();
  },
  // 选择人数范围
  bindPickerPsnNumChange: function(e) {
    let low = this.data.PsnNumArray[0][e.detail.value[0]];
    let up = this.data.PsnNumArray[1][e.detail.value[1]];
    let lowUp = low + '-' + up;
    if (low<=up){
      this.setData({
        collage_numberVal: lowUp,
        collage_number_min: low,
        collage_number_max: up
      })
    }else{
      wx.showToast({
        title: '上限必须大于下限请重新选择',
        icon:'none'
      })
    }
  
  },
  // 选择开始时间
  bindPickerStartDTChange: function(e) {
    console.log(e);
    let year, month, day, hour, minute;
    year = this.data.DateArray[0][e.detail.value[0]];
    month = this.data.DateArray[1][e.detail.value[1]];
    day = this.data.DateArray[2][e.detail.value[2]];
    hour = this.data.DateArray[3][e.detail.value[3]];
    minute = this.data.DateArray[4][e.detail.value[4]];
    let dateTimeVal = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    console.log(dateTimeVal);
    this.setData({
      dateStartTimeVal: dateTimeVal
    })

  },
  // 选择结束时间
  bindPickerEndDTChange: function(e) {
    let year, month, day, hour, minute;
    year = this.data.DateArray[0][e.detail.value[0]];
    month = this.data.DateArray[1][e.detail.value[1]];
    day = this.data.DateArray[2][e.detail.value[2]];
    hour = this.data.DateArray[3][e.detail.value[3]];
    minute = this.data.DateArray[4][e.detail.value[4]];
    let dateTimeVal = year + '-' + month + '-' + day + ' ' + hour + ':' + minute;
    console.log(dateTimeVal);
    this.setData({
      dateEndTimeVal: dateTimeVal
    })

  },
  // 选择费用
  bindPickerBMMChange: function(e) {
    this.setData({
      price_val: this.data.BMMoney[e.detail.value].price,
      price_id: this.data.BMMoney[e.detail.value].id,
    })
  },
  keyPriceFY:function(e){
    this.setData({
      price_val: e.detail.value,
     
    })
  },
  keyRemark:function(e){
    this.setData({
      remarkData:e.detail.value
    });

  },
  // NextBtn下一步
  NextBtn: function(e) {

    let that = this;
    $.POST({
      url:'caJRA',
      data:{
        title: that.data.title,
        pic_id: that.data.pic_id,
        collage_id: that.data.collage_id,
        collage_number_min: that.data.collage_number_min,
        collage_number_max: that.data.collage_number_max,
        register_end_time: '',
        begin_time: that.data.dateStartTimeVal,
        end_time: that.data.dateEndTimeVal,
        type: that.data.type,
        signup_price: that.data.price_val,
        address: that.data.address,
        detailed_address: that.data.detailed_address,
        address_x: that.data.address_x,
        address_y: that.data.address_y,
        remark: that.data.remarkData,
      }
    },function(e){
        console.log(e);
        if(e.code=='200'){
          that.setData({
            tabIndex: 1,
          })
        }
        if(e.code=='300'){
          wx.showToast({
            title: e.msg,
            icon:'none',
          })
        }
    },function(e){
      console.log(e)
    });
    // if (that.data.TopImage) {
    //   if (that.data.title.length >= 5 && that.data.title.length < 20) {
    //     if (that.data.typeVal) {
    //       if (that.data.collage_val) {
    //         if (that.data.collage_numberVal) {
    //           if (that.data.dateStartTimeVal) {
    //             if (that.data.dateEndTimeVal) {
    //               if (that.data.price_val) {
    //                 that.setData({
    //                   tabIndex: 1,
    //                 })
    //               } else {
    //                 wx.showToast({
    //                   title: '请选择报名费',
    //                   icon: 'none'
    //                 })
    //               }
    //             } else {
    //               wx.showToast({
    //                 title: '请选择结束时间',
    //                 icon: 'none'
    //               })
    //             }
    //           } else {
    //             wx.showToast({
    //               title: '请选择开始时间',
    //               icon: 'none'
    //             })
    //           }
    //         } else {
    //           wx.showToast({
    //             title: '请选择人数',
    //             icon: 'none'
    //           })
    //         }
    //       } else {
    //         wx.showToast({
    //           title: '请选择类别',
    //           icon: 'none'
    //         })
    //       }

    //     } else {
    //       wx.showToast({
    //         title: '请选择活动地点',
    //         icon: 'none'
    //       })
    //     }

    //   } else {
    //     wx.showToast({
    //       title: '标题大于5小于20个字',
    //       icon: 'none'
    //     })
    //   }
    // } else {
    //   wx.showToast({
    //     title: '请选择海报',
    //     icon: 'none',
    //   })
    // }
    
   
  },
  // showHDFBody
  showHDFBody: function(e) {
    let that = this;
    that.setData({
      HDFBodyStatus: true,
    })
  },
  //canlePage
  canlePage: function(e) {
    let that = this;
    that.setData({
      HDFBodyStatus: false,
    })
  },
  // updateImage 上传图片
  updateImage: function(e) {
    let self = this;

    let nowListCData = self.data.content;
    let nowListCDataNum = nowListCData.length;
    if (!nowListCDataNum) {
      nowListCDataNum = 0;
    }


    console.log(self.data.image);
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0];
        console.log(self.data.image);
        if (src) {
          // 将图片参数传递给插件
          // $.openWin({
          //   url: '../cropperPoster/cropperPoster',
          //   data: {
          //     CutPageType: 'publishActivitie',
          //     url: src,
          //     content: self.data.content,
          //   }
          // });
          let psnkey = wx.getStorageSync('psnkey');
          wx.uploadFile({
            url: config.Config.updateImgUrl,
            filePath: src,
            name: 'file',
            formData: {
              'key': psnkey
            },
            success(res) {

              let pic = JSON.parse(res.data).data.pic;
       
              let newListCData = self.data.content;
                let ListCDataItem = {
                  type: 'img',
                  text: '',
                  url: pic,

                }
                newListCData.push(ListCDataItem);
                 self.setData({
                  content: newListCData,
                  HDFBodyStatus: false,
                })
            

            }
          });
        }

      },
      fail(res) {

      }
    })
  },
  // addTextf增加文本
  addText: function(e) {
    let nowListCData = this.data.content;
    nowListCData.push({
      type: 'txt',
      text: '',
      url: '',
    });
    this.setData({
      content: nowListCData,
      HDFBodyStatus: false,
    })
  },
  //填写展示的文本
  HDAddChange: function(e) {
    console.log(e);
    let nowListCData = this.data.content;
    nowListCData[e.currentTarget.dataset.index].text = e.detail.value;
    this.setData({
      content: nowListCData,
      HDFBodyStatus: false,
    })
  },
  // delItem删除
  delItem: function(e) {
    console.log(e);
    let index = e.currentTarget.dataset.index;
    let newListCData = this.data.content;
    newListCData = $.delArray(newListCData, index);
    this.setData({
      content: newListCData,
    })
  },
  // preview 预览
  preview: function(e) {
    let that = this;
    wx.setStorage({
      key: 'ActiveData',
      data: that.data,
      success: function(res) {
        $.openWin({
          url: '../collageDetail/collagePreview',
        })
      }
    })

  },
  // release 发布
  release: function(e) {
    let that = this;
    let contentTxt = JSON.stringify(that.data.content);
    $.POST({
      url: 'caRC',
      data: {
        title: that.data.title,
        pic_id: that.data.pic_id,
        collage_id: that.data.collage_id,
        collage_number_min: that.data.collage_number_min,
        collage_number_max: that.data.collage_number_max,
        register_end_time: '',
        begin_time: that.data.dateStartTimeVal,
        end_time: that.data.dateEndTimeVal,
        type: that.data.type,
        signup_price: that.data.price_val,
        address: that.data.address,
        detailed_address: that.data.detailed_address,
        address_x: that.data.address_x,
        address_y: that.data.address_y,
        remark: that.data.remarkData,
        content: contentTxt,
      }
    }, function(res) {
      console.log(res.data);
      that.setData({
        sendRequire: res.data
      })
      if (res.data.status == 1) {
        that.setData({
          HDFZFStatus: true,
        });
      } else {
        that.setData({
          HDFZFStatus: false,
          tabIndex: 2,
        });
        wx.setStorage({
          key: 'ActiveData',
          data: '',
        })
      }

    }, function(res) {

    })

  },
  // 确认支付
  sureBtnZF: function(e) {
    let that = this;
    console.log(that.data.sendRequire);
    $.openWin({
      url: '../payPage/payPage',
      data: {
        page: 'publishActivitie',
        id: that.data.sendRequire.caid,
        price: that.data.sendRequire.price,
        title: that.data.sendRequire.title,
      }
    })

    // that.setData({
    //   HDFZFStatus: false,
    //   tabIndex: 2,
    // });
  },
  // 取消canleHDFZFage
  canleHDFZFage: function(e) {
    let that = this;
    that.setData({
      HDFZFStatus: false,
    });
  },
  // 完成
  FinishBtn: function(e) {
    wx.switchTab({
      url: '../collage/collage',
    })
  },
  // TitleChanger 标题
  TitleChanger: function(e) {
    let TitleVal = e.detail.value;
    let that = this;
    that.setData({
      title: TitleVal,
    })
  },
  // FORMID
  formSubmit: function (e) {
    $.POST({
      url: 'caUserAFI',
      data: {
        formid: e.detail.formId,
      }
    }, function (e) {

    }, function () {

    })
  }

})